namespace Webbness.Generator.CodeDom

import System
import System.Collections
import System.Data.OleDb

public class SQLDbTypeConverter(IDBTypeConverter):

	private _dbtypes as Hashtable

	private _name as string

	private _typeArray as (string) = array(string, 206)

	public def constructor():
		_dbtypes = Hashtable()
		_name = 'SQL Datatype Converter'
		InitTypes()
		NewTypes()

	private def NewTypes():
		_typeArray[0] = null
		//  adEmpty
		_typeArray[2] = 'SMALLINT'
		//  adSmallInt
		_typeArray[3] = 'INTEGER'
		//  adInteger
		_typeArray[4] = 'REAL'
		//  adSingle
		_typeArray[5] = 'FLOAT'
		//  adDouble
		_typeArray[6] = 'MONEY'
		//  adCurrency
		_typeArray[7] = null
		//  adDate
		_typeArray[8] = null
		//  adBSTR
		_typeArray[9] = null
		//  adIDispatch
		_typeArray[10] = null
		//  adError
		_typeArray[11] = 'BIT'
		//  adBoolean
		_typeArray[12] = 'SQL_VARIANT'
		//  adVariant
		_typeArray[13] = null
		//  adIUnknown
		_typeArray[14] = null
		//  adDecimal
		_typeArray[16] = null
		//  adTinyInt
		_typeArray[17] = 'TINYINT'
		//  adUnsignedTinyInt
		_typeArray[18] = null
		//  adUnsignedSmallInt
		_typeArray[19] = null
		//  adUnsignedInt
		_typeArray[20] = 'BIGINT'
		//  adBigInt
		_typeArray[21] = null
		//  adUnsignedBigInt
		_typeArray[64] = null
		//  adFileTime
		_typeArray[72] = 'UNIQUEIDENTIFIER'
		//  adGUID
		_typeArray[128] = 'BINARY'
		//  adBinary
		_typeArray[129] = 'CHAR'
		//  adChar
		_typeArray[130] = 'NCHAR'
		//  adWChar
		_typeArray[131] = 'DECIMAL'
		//  adNumeric
		_typeArray[132] = null
		//  adUserDefined
		_typeArray[133] = null
		//  adDBDate
		_typeArray[134] = null
		//  adDBTime
		_typeArray[135] = 'DATETIME'
		//  adDBTimeStamp
		_typeArray[136] = null
		//  adChapter
		_typeArray[137] = null
		//  adDBFileTime
		_typeArray[138] = null
		//  adPropVariant
		_typeArray[139] = null
		//  adVarNumeric
		_typeArray[200] = 'VARCHAR'
		//  adVarChar
		_typeArray[201] = 'TEXT'
		//  adLongVarChar
		_typeArray[202] = 'NVARCHAR'
		//  adVarWChar
		_typeArray[203] = 'NTEXT'
		//  adLongVarWChar
		_typeArray[204] = 'VARBINARY'
		//  adVarBinary
		_typeArray[205] = 'IMAGE'
		//  adLongVarBinary

	private def InitTypes():
		DBTypes.Add('guid', 'UNIQUEIDENTIFIER')
		DBTypes.Add('dbtimestamp', 'DATE')
		DBTypes.Add('char', 'CHAR')
		DBTypes.Add('integer', 'INTEGER')
		DBTypes.Add('int', 'INTEGER')
		DBTypes.Add('bigint', 'BIGINT')
		DBTypes.Add('boolean', 'BIT')
		DBTypes.Add('bit', 'BIT')
		DBTypes.Add('numeric', 'REAL')
		DBTypes.Add('single', 'REAL')
		DBTypes.Add('double', 'REAL')
		DBTypes.Add('binary', 'BINARY')
		DBTypes.Add('varchar', 'VARCHAR')
		DBTypes.Add('wchar', 'CHAR')
		DBTypes.Add('decimal', 'REAL')
		DBTypes.Add('currency', 'REAL')
		DBTypes.Add('real', 'REAL')
		DBTypes.Add('money', 'REAL')
		DBTypes.Add('smallint', 'INTEGER')
		DBTypes.Add('tinyint', 'INTEGER')
		DBTypes.Add('datetime', 'DATETIME')
		DBTypes.Add('smalldatetime', 'DATETIME')
		DBTypes.Add('image', 'IMAGE')
		DBTypes.Add('text', 'TEXT')
		DBTypes.Add('nvarchar', 'VARCHAR')
		DBTypes.Add('ntext', 'TEXT')
		DBTypes.Add('nchar', 'CHAR')
		DBTypes.Add('uniqueidentifier', 'UNIQUEIDENTIFIER')
		DBTypes.Add('float', 'FLOAT')
		DBTypes.Add('varbinary', 'VARBINARY')
		//TODO: PRIO 2: Check out, if there are more types needed
		// and if these type converts above works fine

	#region IDBTypeConverter Member
	public DBTypes as System.Collections.Hashtable:
		get:
			return _dbtypes
		set:
			_dbtypes = value

	public ConvertName as string:
		get:
			return _name
		set:
			pass

	public def Convert(dbtype as string) as string:
		if DBTypes.ContainsKey(dbtype.ToLower()):
			#Console.WriteLine(((('Mapped ' + dbtype.ToLower()) + ' to ') + DBTypes[dbtype.ToLower()].ToString()))
			return DBTypes[dbtype.ToLower()].ToString()
		else:
			#Console.WriteLine(((('\tUnable to map ' + dbtype.ToLower()) + ' to ') + DBTypes[dbtype.ToLower()]))
			raise System.ApplicationException(('Unhandled SQL Data Type: ' + dbtype))

	public def Convert(OleDbId as int) as string:
		#Console.WriteLine((((((('Mapped ' + cast(OleDbType, OleDbId).ToString()) + ' (') + OleDbId.ToString()) + ') ') + ' to ') + _typeArray[OleDbId]))
		return _typeArray[OleDbId]
	#endregion
